﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xajhzwb.Components.Data;
using System.Threading;
using Xajhzwb.Components.Helper;
using Xajhzwb.Components.Util;

namespace Xajhzwb.Components.Publish
{
    public class UploadTask
    {
        public int TaskID;
        public TaskData TaskData = new TaskData();

        public UploadTask(int taskID, TaskData taskData)
        {
            TaskID = taskID;
            TaskData = taskData;
        }

        ~UploadTask()
        {

        }

        #region Event
        private readonly Object m_eventLock = new Object();

        private event EventHandler<UploadCompletedEventArgs> e_UploadCompleted;
        internal event EventHandler<UploadCompletedEventArgs> UploadCompleted
        {
            add { lock(m_eventLock) { e_UploadCompleted += value; } }
            remove { lock(m_eventLock) { e_UploadCompleted -= value; } }
        }

        private event EventHandler<UploadFailedEventArgs> e_UploadFailed;
        internal event EventHandler<UploadFailedEventArgs> UploadFailed
        {
            add { lock(m_eventLock) { e_UploadFailed += value; } }
            remove { lock(m_eventLock) { e_UploadFailed -= value; } }
        }

        private event EventHandler<UploadStartedEventArgs> e_UploadStarted;
        internal event EventHandler<UploadStartedEventArgs> UploadStarted
        {
            add { lock(m_eventLock) { e_UploadStarted += value; } }
            remove { lock(m_eventLock) { e_UploadStarted -= value; } }
        }

        private event EventHandler<UploadErrorEventArgs> e_UploadError;
        internal event EventHandler<UploadErrorEventArgs> UploadError
        {
            add { lock(m_eventLock) { e_UploadError += value; } }
            remove { lock(m_eventLock) { e_UploadError -= value; } }
        }

        private event EventHandler<UploadLogEventArgs> e_UploadLog;
        internal event EventHandler<UploadLogEventArgs> UploadLog
        {
            add { lock(m_eventLock) { e_UploadLog += value; } }
            remove { lock(m_eventLock) { e_UploadLog -= value; } }
        }

        //private event EventHandler<cRunTaskEventArgs> e_RunTask;
        //internal event EventHandler<cRunTaskEventArgs> RunTask
        //{
        //    add { lock(m_eventLock) { e_RunTask += value; } }
        //    remove { lock(m_eventLock) { e_RunTask -= value; } }
        //}
        #endregion

        private Thread m_Thread;
        private readonly Object m_threadLock = new Object();

        public void startPublic()
        {
            lock(m_threadLock)
            {
                m_Thread = new Thread(this.ThreadWork);
                m_Thread.Start();
            }
        }

        private void ThreadWork()
        {
            try
            {
                doFileUpload();
            }
            catch(System.Exception ex)
            {
                e_UploadError(this, new UploadErrorEventArgs(this.TaskData.TaskID, this.TaskData.TaskName, ex));
            }
        }

        private bool doFileUpload()
        {
            UploadStartedEventArgs evt = new UploadStartedEventArgs(this.TaskData.TaskID, this.TaskData.TaskName);
            e_UploadStarted(this, evt);

            // upload
            UploadFiles();

            UploadCompletedEventArgs evt1 = new UploadCompletedEventArgs(this.TaskData.TaskID, this.TaskData.TaskName);
            e_UploadCompleted(this, evt1);

            return true;
        }

        private void UploadFiles()
        {
            if(TaskData.UploadFile != null && TaskData.UploadFile.Count > 0)
            {
                try
                {
                    FtpHelper ftp = new FtpHelper(TaskData.PublishData.FtpServer, TaskData.PublishData.FtpPort,
                                                  TaskData.PublishData.FtpUser, TaskData.PublishData.FtpPass, TaskData.PublishData.IsFtpPasv);
                    ftp.ChangeDir(TaskData.PublishData.FtpDir);

                    foreach(string uploadFile in TaskData.UploadFile)
                    {
                        ftp.OpenUpload(uploadFile, true);
                    }
                }
                catch(Exception ex)
                {
                    if(e_UploadLog != null)
                    {
                        WriteLog(ex.Message);
                        e_UploadLog(this, new UploadLogEventArgs(this.TaskData.TaskID, ((int)Const.LogType.Error).ToString() + "任务：" + this.TaskData.TaskName + "上传失败，错误信息为：" + ex.Message + "\n"));
                    }

                    return;

                }
            }
        }

        private void WriteLog(string logMessage)
        {
            LogHelper.WriteLog(TaskData.TaskName, LogHelper.LogType.UploadError, logMessage);
        }

    }
}
